package com.sunfeng.n5;

import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunfeng    可见性问题
 * @date 2021/04/13 15:44
 **/

@Slf4j(topic = "c.Test1")
public class Test1 {
    static boolean run = true;

    // 演示 synchronized

    final static Object lock = new Object();

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    if (!run) {
                        break;
                    }
                }
            }
        }, "t1");
        t.start();

        TimeUtils.sleep(1);
        log.debug("停止T");
        synchronized (lock) {
            run = false;
        }
    }
}
